package cn.qzuie.smart.module.dorm.service.dormitories;

import cn.hutool.core.collection.CollUtil;
import cn.qzuie.smart.module.dorm.dal.dataobject.buildings.BuildingsDO;
import cn.qzuie.smart.module.dorm.dal.mysql.buildings.BuildingsMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.qzuie.smart.module.dorm.controller.admin.dormitories.vo.*;
import cn.qzuie.smart.module.dorm.dal.dataobject.dormitories.DormitoriesDO;
import cn.qzuie.smart.framework.common.pojo.PageResult;
import cn.qzuie.smart.framework.common.pojo.PageParam;
import cn.qzuie.smart.framework.common.util.object.BeanUtils;

import cn.qzuie.smart.module.dorm.dal.mysql.dormitories.DormitoriesMapper;

import static cn.qzuie.smart.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.qzuie.smart.framework.common.util.collection.CollectionUtils.convertList;
import static cn.qzuie.smart.framework.common.util.collection.CollectionUtils.diffList;
import static cn.qzuie.smart.module.dorm.enums.ErrorCodeConstants.*;

/**
 * 宿舍 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DormitoriesServiceImpl implements DormitoriesService {

    @Resource
    private DormitoriesMapper itoriesMapper;

    @Resource
    private BuildingsMapper buildingsMapper;

    @Override
    public Integer createitories(DormitoriesSaveReqVO createReqVO) {
        // 插入
        DormitoriesDO itories = BeanUtils.toBean(createReqVO, DormitoriesDO.class);
        itoriesMapper.insert(itories);

        // 返回
        return itories.getId();
    }

    @Override
    public void updateitories(DormitoriesSaveReqVO updateReqVO) {
        // 校验存在
        validateitoriesExists(updateReqVO.getId());
        // 更新
        DormitoriesDO updateObj = BeanUtils.toBean(updateReqVO, DormitoriesDO.class);
        itoriesMapper.updateById(updateObj);
    }

    @Override
    public void deleteitories(Long id) {
        // 校验存在
        validateitoriesExists(id);
        // 删除
        itoriesMapper.deleteById(id);
    }

    @Override
        public void deleteitoriesListByIds(List<Long> ids) {
        // 删除
        itoriesMapper.deleteByIds(ids);
        }


    private void validateitoriesExists(Long id) {
        if (itoriesMapper.selectById(id) == null) {
            throw exception(ITORIES_NOT_EXISTS);
        }
    }

    @Override
    public DormitoriesDO getitories(Long id) {
        return itoriesMapper.selectById(id);
    }

    @Override
    public PageResult<DormitoriesDO> getitoriesPage(DormitoriesPageReqVO pageReqVO) {
        return itoriesMapper.selectPage(pageReqVO);
    }

}